package edu.buffalo.cse.cse486_586.provider;

import java.util.HashMap;

import edu.buffalo.cse.cse486_586.util.AppUtil;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MessageProvider extends ContentProvider {
	// Debugging
	private static final String TAG = "MessageProvider";
	//private static final boolean D = true;

	private static final UriMatcher sUriMatcher;
	private static final int MSG_TYPE_LIST = 1;
	private static final int MSG_TYPE_ONE = 2;

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AppUtil.AUTHORITY, AppUtil.PATH, MSG_TYPE_LIST);
		//sUriMatcher.addURI(AppUtil.AUTHORITY, AppUtil.PATH + "/#", MSG_TYPE_ONE);
	}

	private static final HashMap<String, String> sMessagesProjectionMap;
	static {
		sMessagesProjectionMap = new HashMap<String, String>();
		sMessagesProjectionMap.put(AppUtil.MessageTable.COL_ID, AppUtil.MessageTable.COL_ID);
		sMessagesProjectionMap.put(AppUtil.MessageTable.COL_PROVIDER_KEY,
				AppUtil.MessageTable.COL_PROVIDER_KEY);
		sMessagesProjectionMap.put(AppUtil.MessageTable.COL_PROVIDER_VALUE,
				AppUtil.MessageTable.COL_PROVIDER_VALUE);
	}

	private MessageDbHelper mDbHelper;

	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case MSG_TYPE_LIST:
			count = db.delete(AppUtil.MessageTable.MSG_TABLE, where, whereArgs);
			break;
		case MSG_TYPE_ONE:
			String rowId = uri.getPathSegments().get(1);
			count = db.delete(AppUtil.MessageTable.MSG_TABLE, AppUtil.MessageTable.COL_ID + " = "
					+ rowId + (!TextUtils.isEmpty(where) ? " AND (" + where + ")" : ""), whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case MSG_TYPE_LIST:
			return AppUtil.CONTENT_TYPE_ARTICLES_LIST;

		case MSG_TYPE_ONE:
			return AppUtil.CONTENT_TYPE_ARTICLE_ONE;

		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		if (sUriMatcher.match(uri) != MSG_TYPE_LIST) {
			throw new IllegalArgumentException("[Insert](01)Unknown URI: " + uri);
		}

		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long rowId = db.insert(AppUtil.MessageTable.MSG_TABLE, null, values);
		if (rowId > 0) {
			Uri msgUri = ContentUris.withAppendedId(AppUtil.CONTENT_URI, rowId);
			getContext().getContentResolver().notifyChange(msgUri, null);
			return msgUri;
		}
		throw new IllegalArgumentException("[Insert](02)Unknown URI: " + uri);
	}

	@Override
	public boolean onCreate() {
		Log.d(TAG, "on create");
		mDbHelper = new MessageDbHelper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
		switch (sUriMatcher.match(uri)) {
		case MSG_TYPE_LIST:
			builder.setTables(AppUtil.MessageTable.MSG_TABLE);
			builder.setProjectionMap(sMessagesProjectionMap);
			break;

		case MSG_TYPE_ONE:
			builder.setTables(AppUtil.MessageTable.MSG_TABLE);
			builder.setProjectionMap(sMessagesProjectionMap);
			builder.appendWhere(AppUtil.MessageTable.COL_ID + " = " + uri.getPathSegments().get(1));
			break;

		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}

		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor queryCursor = builder.query(db, projection, selection, selectionArgs, null, null,
				null);
		queryCursor.setNotificationUri(getContext().getContentResolver(), uri);

		return queryCursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {

		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case MSG_TYPE_LIST:
			count = db.update(AppUtil.MessageTable.MSG_TABLE, values, where, whereArgs);
			break;

		case MSG_TYPE_ONE:
			String rowId = uri.getPathSegments().get(1);
			count = db.update(AppUtil.MessageTable.MSG_TABLE, values, AppUtil.MessageTable.COL_ID
					+ " = " + rowId + (!TextUtils.isEmpty(where) ? " AND (" + ")" : ""), whereArgs);

		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
